package com.song.controller;


import com.google.common.collect.PeekingIterator;
import com.song.entity.vo.InvestVO;
import com.song.hfb.RequestHelper;
import com.song.result.Result;
import com.song.service.LendItemService;
import com.song.util.JwtUtils;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * <p>
 * 标的出借记录表 前端控制器
 * </p>
 *
 * @author Song
 * @since 2025-01-03
 */
@RestController
@RequestMapping("/api/core/lendItem")
public class LendItemController {

    @Autowired
    private LendItemService lendItemService;

    //http://localhost/api/core/lendItem/auth/commitInvest
    @ApiOperation("会员投资提交数据")
    @PostMapping("/auth/commitInvest")
    public Result commitInvest(@RequestBody InvestVO investVO, HttpServletRequest request){
        //获取投资用户信息
        String token = request.getHeader("token");
        Long userId = JwtUtils.getUserId(token);
        String userName = JwtUtils.getUserName(token);

        investVO.setInvestUserId(userId);
        investVO.setInvestName(userName);

        String formStrs= lendItemService.commitInvest(investVO);
        return Result.ok().data("formStr",formStrs);

    }


    /**
     * 投资成功之后的回调方法
     */
    //  http://localhost/api/core/lendItem/notify";
    @PostMapping("/notify")
    public String notifyss(HttpServletRequest request){
        Map<String, Object> objectMap = RequestHelper.switchMap(request.getParameterMap());

        if(RequestHelper.isSignEquals(objectMap)){
            if("0001".equals(objectMap.get("resultCode"))){
                lendItemService.notifyss(objectMap);
            }else{
                return  "fail";
            }
        }else{
            System.out.println("回调签名存在问题.....");
            return  "fail";
        }

        return  "success";
    }


}

